<template>
    <div class="index-main">
    <a-spin size="large" tip="Loading..." style="position:sticky;top:30%;width:100%;height:100%;" :spinning="spinner">
        <indexNavs ref="navs" />
        <searchBox />
        <a-modal v-model="visibleUser" :closable="false" :keyboard="false" :maskClosable="false" width="20%" title="消息" @ok="login">
            <template slot="footer"><a-button key="submit" type="primary" @click="ConfirmError">确认</a-button></template>
            <p class="failure">获取用户信息出错，请联系管理员！</p>
        </a-modal>
        <a-modal v-model="visibleLook" :closable="false" :keyboard="false" :maskClosable="false" width="20%" title="消息" @ok="logout">
            <template slot="footer"><a-button key="submit" type="primary" @click="logout">确认</a-button></template>
            <p class="failure">您无权访问此站点！</p>
        </a-modal>
    </a-spin>
    </div>
</template>

<script>
    import axios from 'axios'
    import indexNavs from "./indexNavs"
    import searchBox from './searchBox'
    import { mapActions } from 'vuex'
    import { accountAuthtication } from '../../api/urls'

    export default {
        components:{
            indexNavs,
            searchBox,
        },

        data(){
            return{
                visibleUser: false,
                spinner: false,
                visibleLook: false,
            }
        },

        created() {
            this.checkUser()
        },

        computed:{
            // getStatus(){
            //     return this.$store.state.app.status
            // }
        },

        mounted() {
            //this.query()
        },

        
        methods:{
            async checkUser() {
                this.spinner = true
                /* 
                判断是否有传入token
                登录：https://www.cnbksy.com/jdLogin?targetUrl=https://tushu.cnbksy.com?ss
                返回token：https://jdts.library.sh.cn/?ss&token=6fd70427739110ab86b4a2f427423bb2e0c1cc2707665536f676fb4dda7a7ff6af8503d1c564ea39f86392a3848ec58ee7590403f3aee5947c0f9838e5f61396#/
                router(路由配置) 需修改为 mode: 'history' 模式
                通过 this.$route.query.xxx 获取
                */
                if(!sessionStorage.getItem('token')) {
                    sessionStorage.setItem('token', this.$route.query.token||'')
                }
                if(sessionStorage.getItem('token')&&!sessionStorage.getItem('orgName')) {
                    await axios.post(`${accountAuthtication}?token=`+ sessionStorage.getItem('token')).then(response => {
                        if (response.data.result==0) {
                            console.log(response.data.data)
                            console.log(response.data.data.userName)
                            this.$refs.navs.orgName = response.data.data.userName
                            sessionStorage.setItem('orgName', response.data.data.userName)
                            this.$router.push({name: 'index'})
                        } else {
                            this.visibleUser = true
                        }
                    });
                }
                if(sessionStorage.getItem('token')&&sessionStorage.getItem('orgName')) {
                    this.$router.push({name: 'index'})
                }
                this.spinner = false
            },

            login() {
                window.location = 'https://dhapi.library.sh.cn/management/webapi/cenofmagent/token/locpage?systemId=20&durl=https://jdts.library.sh.cn/index'
            },

            ConfirmError(){
                this.visibleUser = false
            },
            async logout() {
                this.spinner = true
                this.visibleLook = false
                let token = sessionStorage.getItem('token')
                sessionStorage.setItem('token', '')
                sessionStorage.setItem('orgName', '')
                // sessionStorage.setItem('ipAddress', '')
                // sessionStorage.setItem('indexPermission', '')
                // sessionStorage.setItem('viewPermission', '')
                // if(token) {
                //     await axios.get("https://www.cnbksy.com/jdLogout",{params:{token: token}}).then(response => {
                //         window.location = 'https://www.cnbksy.com/jdLogin?targetUrl=https://tushu.cnbksy.com?ss'
                //     });
                // } else {
                //     window.location = 'https://www.cnbksy.com/jdLogin?targetUrl=https://tushu.cnbksy.com?ss'
                // }
                
                this.$router.push({
                    name: 'index'
                })
                location.reload()
            },

            quit() {
                window.opener = null;
                window.open("about:blank", "_self").close()
            },

            // ...mapActions(['setStatus']),
        }
    }
</script>

<style>
    .ant-spin-container{
        position: unset;
    }
</style>
<style lang="less" scoped>
    .index-main{
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background-size: cover;
        background-image: url("img/bg.jpg");
        background-repeat: no-repeat;
    }
    .failure {
        font-size: 1.3rem;
        color: #9a8686;
        margin: 0 auto;
    }
    .ant-btn-primary {
        background-color: #661112;
        border-color: #661112;
    }
    // .ant-modal-title {
    //     color: #440000d9;
    //     font-size: 1.5rem;
    // }
</style>
